<!--
 * @Author: cmm seonhu@126.com
 * @Date: 2022-11-08 10:42:45
 * @LastEditors: cmm seonhu@126.com
 * @LastEditTime: 2022-11-11 09:42:31
 * @FilePath: \knife4j-vue\src\components\MiniProfiler\index.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template></template>

<script>
import axios from "axios";

export default {
  name: "MiniProfiler",
  created() {
    const that = this;

    // 响应数据拦截器
    const key = "MiniProfiler";
    axios.interceptors.response.use(
      (response) => {
        const miniprofilerIdsString = response.headers["x-miniprofiler-ids"];
        if (
          miniprofilerIdsString !== undefined &&
          miniprofilerIdsString !== null &&
          miniprofilerIdsString !== ""
        ) {
          const miniProfilerIds = JSON.parse(miniprofilerIdsString);
          const miniProfiler = window[key];
          if (miniProfiler !== undefined && miniProfiler !== null) {
            miniProfiler.fetchResults(miniProfilerIds);
          }
        }
        return response;
      },
      (error) => {
        const miniprofilerIdsString =
          error.response.headers["x-miniprofiler-ids"];
        if (
          miniprofilerIdsString !== undefined &&
          miniprofilerIdsString !== null &&
          miniprofilerIdsString !== ""
        ) {
          const miniProfilerIds = JSON.parse(miniprofilerIdsString);
          const miniProfiler = window[key];
          if (miniProfiler !== undefined && miniProfiler !== null) {
            miniProfiler.fetchResults(miniProfilerIds);
          }
        }
        return Promise.reject(error);
      }
    );
    that.$axios.interceptors.response.use(
      (response) => {
        const miniprofilerIdsString = response.headers["x-miniprofiler-ids"];
        if (
          miniprofilerIdsString !== undefined &&
          miniprofilerIdsString !== null &&
          miniprofilerIdsString !== ""
        ) {
          const miniProfilerIds = JSON.parse(miniprofilerIdsString);
          const miniProfiler = window[key];
          if (miniProfiler !== undefined && miniProfiler !== null) {
            miniProfiler.fetchResults(miniProfilerIds);
          }
        }
        var data = response.data;
        return data;
      },
      (error) => {
        const miniprofilerIdsString =
          error.response.headers["x-miniprofiler-ids"];
        if (
          miniprofilerIdsString !== undefined &&
          miniprofilerIdsString !== null &&
          miniprofilerIdsString !== ""
        ) {
          const miniProfilerIds = JSON.parse(miniprofilerIdsString);
          const miniProfiler = window[key];
          if (miniProfiler !== undefined && miniProfiler !== null) {
            miniProfiler.fetchResults(miniProfilerIds);
          }
        }
        return Promise.reject(error);
      }
    );

    // 创建Miniprofier需要的js和css
    const body = document.body;
    const script = document.createElement("script");
    script.setAttribute("async", "true");
    script.setAttribute("id", "mini-profiler");
    script.setAttribute("src", "/profiler/includes.min.js");
    script.setAttribute("data-version", "");
    script.setAttribute("data-path", "/profiler/");
    script.setAttribute("data-current-id", "");
    script.setAttribute("data-ids", "");
    script.setAttribute("data-position", "Right");
    script.setAttribute("data-scheme", "");
    script.setAttribute("data-authorized", "true");
    script.setAttribute("data-max-traces", "15");
    script.setAttribute("data-toggle-shortcut", "Alt+P");
    script.setAttribute("data-trivial-milliseconds", "2.5");
    script.setAttribute(
      "data-ignored-duplicate-execute-types",
      "Open,OpenAsync,Close,CloseAsync"
    );
    script.setAttribute("data-trivial", "true");
    script.setAttribute("data-children", "true");
    script.setAttribute("data-controls", "true");
    script.setAttribute("data-start-hidden", "false");
    body.appendChild(script);

    // const css = document.createElement("link");
    // css.href = "/profiler/includes.min.css";
    // css.rel = "stylesheet";
    // body.appendChild(css);
  },
};
</script>
